﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_HisPosition : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private Classes.Main main = new Classes.Main();
        private HisPositionDAL pos = new HisPositionDAL();
        private CommandTypeBLL cmdType = new CommandTypeBLL();
        private EmployeeBLL emp = new EmployeeBLL(); 
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            //LoadCmbCmdTypeSeq();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSequence.Text = DataBinder.Eval(DataItem, "pos_his_seq").ToString();
                    DataSet ds;
                    DataRowCollection drc;

                    ds = pos.LoadHisPositions(Convert.ToInt32(lblSequence.Text), null);
                    drc = ds.Tables[0].Rows;
                    if (drc.Count > 0)
                    {
                        lblEmpSeq.Text = drc[0]["emp_seq"].ToString();
                        lblCmdDtlsSeq.Text = drc[0]["cmd_dtls_seq"].ToString();

                        txtClassName.Text = drc[0]["class_name"].ToString();
                        txtDepartmentName.Text = drc[0]["department_name"].ToString();
                        txtExecName.Text = drc[0]["exec_name"].ToString();
                        txtFieldName.Text = drc[0]["field_name"].ToString();
                        txtJobsiteName.Text = drc[0]["job_site_name"].ToString();
                        txtMinistryName.Text = drc[0]["ministry_name"].ToString();
                        txtPosName.Text = drc[0]["pos_name"].ToString();
                        txtPosRef.Text = drc[0]["pos_ref"].ToString();
                        txtSalary.Text = drc[0]["salary"].ToString();
                        txtWorkingJobsiteName.Text = drc[0]["working_job_site_name"].ToString();

	                    txtCmdName.Text = drc[0]["cmd_name"].ToString();
	                    txtCmdRefNo.Text = drc[0]["cmd_ref_no"].ToString();
	                    txtStartDate.SetValue(drc[0]["start_date"].ToString());
	                    txtEndDate.SetValue(drc[0]["end_date"].ToString());
	                    txtCmdDate.SetValue(drc[0]["cmd_date"].ToString());

	                    //txtNewClassName.Text = drc[0]["new_class_name"].ToString();
	                    //txtNewDepName.Text = drc[0]["new_dep_name"].ToString();
	                    //txtNewExecName.Text = drc[0]["new_exec_name"].ToString();
	                    //txtNewFieldName.Text = drc[0]["new_field_name"].ToString();
	                    //txtNewJobsiteName.Text = drc[0]["new_job_site_name"].ToString();
	                    //txtNewMisName.Text = drc[0]["new_mis_name"].ToString();
	                    //txtNewPosName.Text = drc[0]["new_pos_name"].ToString();
	                    //txtNewPosRef.Text = drc[0]["new_pos_ref"].ToString();
	                    //txtNewSalary.Text = drc[0]["new_salary"].ToString();
	                    //txtNewWorkingJobsiteName.Text = drc[0]["new_working_job_site_name"].ToString();
                    }
                }
            }
        }
        //private void LoadCmbCmdTypeSeq()
        //{
        //    //string strEmpTypeSeq = emp.LoadEmployees(main.ConvertNullInt(lblEmpSeq.Text), "", true, "").Tables[0].Rows[0]["emp_type_seq"].ToString();

        //    cmbCmdTypeSeq.DataSource = cmdType.LoadCommandTypes(null, null);
        //    cmbCmdTypeSeq.DataBind();
        //}
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected bool CheckData()
        {
            bool bOK = true;
            string strError = "";

            //if (cmbCmdTypeSeq.SelectedValue.Trim().Length == 0)
            //{
            //    strError = main.ErrorEmpty("ประเภทคำสั่ง");
            //    bOK = false;
            //}
            //else if (txtCmdRefNo.Text.Trim().Length == 0)
            //{
            //    strError = main.ErrorEmpty("เลขที่คำสั่ง");
            //    bOK = false;
            //}
            //else if (lblEmpSeq.Text.Trim().Length == 0)
            //{
            //    strError = main.ErrorEmpty("บุคลากร");
            //    bOK = false;
            //}

            if (strError.Length > 0)
            {
                main.MessageAlert(Page, strError);
            }

            return bOK;
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            pos.Insert(
				txtCmdName.Text,
				txtCmdRefNo.Text,
				txtStartDate.GetDateNullableValue(),
				txtEndDate.GetDateNullableValue(),
				txtCmdDate.GetDateNullableValue(),

				main.ConvertNullInt(lblEmpSeq.Text.Trim())
                //, main.ConvertNullDateTime(txtStartDate.GetStringValue())
                //, main.ConvertNullDateTime(txtEndDate.GetStringValue())
                , main.ConvertNullInt(lblCmdDtlsSeq.Text)
                //, txtCmdRefNo.Text
                //, main.ConvertNullDateTime(txtCmdDate.GetStringValue())
                //, txtCmdName.Text
                , -1

                , txtPosRef.Text
                , txtPosName.Text
                , txtClassName.Text
                , txtJobsiteName.Text
                , txtWorkingJobsiteName.Text
                , txtDepartmentName.Text
                , txtMinistryName.Text
                , txtFieldName.Text
                , txtExecName.Text
                , main.ConvertNullDouble(txtSalary.Text)

                , ""
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            pos.Update(

				txtCmdName.Text,
				txtCmdRefNo.Text,
				txtStartDate.GetDateNullableValue(),
				txtEndDate.GetDateNullableValue(),
				txtCmdDate.GetDateNullableValue(),
				
				
				Int32.Parse(lblSequence.Text.Trim())
                //, lblEmpSeq.Text.Trim()
                //, main.ConvertNullInt(lblEmpSeq.Text.Trim())
                //, main.ConvertNullDateTime(txtStartDate.GetStringValue())
                //, main.ConvertNullDateTime(txtEndDate.GetStringValue())
                , main.ConvertNullInt(lblCmdDtlsSeq.Text)
                //, txtCmdRefNo.Text
                //, main.ConvertNullDateTime(txtCmdDate.GetStringValue())
                //, txtCmdName.Text
                , -1

                , txtPosRef.Text
                , txtPosName.Text
                , txtClassName.Text
                , txtJobsiteName.Text
                , txtWorkingJobsiteName.Text
                , txtDepartmentName.Text
                , txtMinistryName.Text
                , txtFieldName.Text
                , txtExecName.Text
                , main.ConvertNullDouble(txtSalary.Text)

                , ""
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }
    }
}